//
//  ShareMessageInfo.m
//  ShareCenter
//
//  Created by yutao on 14-2-18.
//  Copyright (c) 2014年 yutao. All rights reserved.
//

#import "ShareMessageInfo.h"
#define SNShareMessageTitle      @"SNShareMessageTitle"
#define SNShareMessageSummary    @"SNShareMessageSummary"
#define SNShareMessageContent    @"SNShareMessageContent"
#define SNShareMessageImageUrl   @"SNShareMessageImageUrl"
#define SNShareMessageVideoStreamUrl   @"SNShareMessageVideoStreamUrl"
#define SNShareMessageVideoSourceUrl   @"SNShareMessageVideoSourceUrl"
#define SNShareMessageImageData  @"SNShareMessageImageData"
#define SNShareMessageImage      @"SNShareMessageImage"
#define SNShareMessageLinkUrl    @"SNShareMessageLinkUrl"

#define SNShareMessageArticleCopyText @"SNShareMessageArticleCopyText"
#define SNShareMessageImageFinalUrl   @"SNShareMessageImageFinalUrl"

@implementation ShareMessageInfo

@synthesize title = _title;
@synthesize summary = _summary;
@synthesize content = _content;
@synthesize imageUrl = _imageUrl;
@synthesize videoStreamUrl = _videoStreamUrl;
@synthesize videoSourceUrl= _videoSourceUrl;
@synthesize image = _image;
@synthesize imageData = _imageData;
@synthesize linkUrl = _linkUrl;
@synthesize articleCopyText = _articleCopyText;
@synthesize imageFinalUrl = _imageFinalUrl;

/*
 * 系列化是为了微博分享中传入 requestInfo
 * 该参数需要传入的数据是支持系列化的.
 */

- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:self.title forKey:SNShareMessageTitle];
    [aCoder encodeObject:self.summary forKey:SNShareMessageSummary];
    [aCoder encodeObject:self.content forKey:SNShareMessageContent];
    [aCoder encodeObject:self.imageUrl forKey:SNShareMessageImageUrl];
    [aCoder encodeObject:self.videoStreamUrl forKey:SNShareMessageVideoStreamUrl];
    [aCoder encodeObject:self.videoSourceUrl forKey:SNShareMessageVideoSourceUrl];
    [aCoder encodeObject:self.image forKey:SNShareMessageImage];
    [aCoder encodeObject:self.linkUrl forKey:SNShareMessageLinkUrl];
    [aCoder encodeObject:self.articleCopyText forKey:SNShareMessageArticleCopyText];
    [aCoder encodeObject:self.imageFinalUrl forKey:SNShareMessageImageFinalUrl];
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super init])
    {
        self.title  = [aDecoder decodeObjectForKey:SNShareMessageTitle];
        self.content = [aDecoder decodeObjectForKey:SNShareMessageContent];
        self.summary = [aDecoder decodeObjectForKey:SNShareMessageSummary];
        self.imageUrl = [aDecoder decodeObjectForKey:SNShareMessageImageUrl];
        self.videoStreamUrl = [aDecoder decodeObjectForKey:SNShareMessageVideoStreamUrl];
        self.videoSourceUrl = [aDecoder decodeObjectForKey:SNShareMessageVideoSourceUrl];
        self.image = [aDecoder decodeObjectForKey:SNShareMessageImage];
        self.linkUrl = [aDecoder decodeObjectForKey:SNShareMessageLinkUrl];
        self.articleCopyText = [aDecoder decodeObjectForKey:SNShareMessageArticleCopyText];
        self.imageFinalUrl = [aDecoder decodeObjectForKey:SNShareMessageImageFinalUrl];
    }
    return self;
}

- (void)dealloc
{
    [_title release],   _title = nil;
    [_summary release], _summary = nil;
    [_content release], _content = nil;
    [_imageUrl release], _imageUrl = nil;
    [_videoStreamUrl release], _videoStreamUrl = nil;
    [_videoSourceUrl release], _videoSourceUrl = nil;
    [_image release],   _image = nil;
    [_imageData release], _imageData = nil;
    [_linkUrl release], _linkUrl = nil;
    [_articleCopyText release], _articleCopyText = nil;
    [_imageFinalUrl release], _imageFinalUrl = nil;
    
    [super dealloc];
}

- (id)initWithTitle:(NSString *)title
            summary:(NSString *)summary
           imageUrl:(NSString *)imageUrl
{
    self = [super init];
    if (self) {
        self.summary = summary;
        self.title = title;
        self.imageUrl = imageUrl;
    }
    
    return self;
}

- (void)setSummary:(NSString *)summary_
{
    if (![_summary isEqualToString:summary_]) {
        // 有的时候summary不是nil但是length==0,这个就会出现逻辑判断错误.
        if ([summary_ length] > 0) {
            [_summary release];
            _summary = [summary_ copy];
        }
    }
}

+ (id)message
{
    ShareMessageInfo *message = [[[self class] alloc] init];
    
    return [message autorelease];
}

+ (id)messageWithTitle:(NSString *)title
               summary:(NSString *)summary
              imageUrl:(NSString *)imageUrl
{
    ShareMessageInfo *message = [[[self class] alloc] initWithTitle:title
                                                          summary:summary
                                                         imageUrl:imageUrl];
    
    return [message autorelease];
}

+ (id)messageWithTitle:(NSString *)title
               summary:(NSString *)summary
              imageUrl:(NSString *)imageUrl
               linkUrl:(NSString *)linkUrl
{
    ShareMessageInfo *message = [[[self class] alloc] initWithTitle:title
                                                          summary:summary
                                                         imageUrl:imageUrl];
    message.linkUrl = linkUrl;
    
    return [message autorelease];
}
@end
